package com.epam.cdp.jpa.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

@Data
@EqualsAndHashCode(callSuper = true)
@ToString(exclude = "children", callSuper = true)
@Entity
public class BookCategory extends BaseEntity {

    private static final long serialVersionUID = 1L;

    private String name;

    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
    private BookCategory parent;

    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
    private List<BookCategory> children;
    
    public void addChild(BookCategory child) {
	if (children == null) {
	    children = new ArrayList<BookCategory>();
	}
	children.add(child);
    }
}
